﻿package 最长公共子串;

/**
 * Created by hest0 on 2017/4/17.
 */
public class 最长公共子串 {
    public static void main(String[] args) {

    }

    public class LongestSubstring {
        public int findLongest(String A, int n, String B, int m) {
            char[] a = A.toCharArray();
            char[] b = B.toCharArray();
            int[][] dp = new int[n][m];

            for (int i = 0; i < n; i++) {
                dp[i][0] = a[i] == b[0] ? 1 : dp[i][0];
            }

            for (int j = 0; j < m; j++) {
                dp[0][j] = a[0] == b[j] ? 1 : dp[0][j];
            }

            //记录最大值
            int max = 0;
            for (int i = 1; i < n; i++) {
                for (int j = 1; j < m; j++) {
                    dp[i][j] = a[i] == b[j] ? dp[i - 1][j - 1] + 1 : dp[i][j];
                    max = Math.max(max, dp[i][j]);
                }
            }

            return max;
        }
    }
}
